<?php include dirname(dirname(__FILE__)).'/admin/common/header.php'; ?>


var css = '.x-grid3-cell-inner em {color:red;}',
    head = document.head || document.getElementsByTagName('head')[0],
    style = document.createElement('style');

style.type = 'text/css';
if (style.styleSheet){
  style.styleSheet.cssText = css;
} else {
  style.appendChild(document.createTextNode(css));
}
head.appendChild(style);

function syntaxHighlight(json) {
	if(typeof stringValue != 'string') {
		json = JSON.stringify(json, undefined, 2);
	}
    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}

var attr = ['label','type'];
var user = <?php echo isset($user)?$user:'""';?>;

//x-grid3-cell-inner

var img = new Ext.Panel({
	html:'<img src="'+list.siteUrl+'ys-pub/resources/images/zm/login-bg.jpg" width="100%" />',
	style:'width:100%;position:absolute'
});

var apiGrid = new ZM.GridPanel({
	title: 'Api接口',
	region: 'center',
	url: list.siteUrl + 'zm/console/getApis',
	onlyShow:true,
	noRowNumber:false,
	remoteSort:false,
	remoteFilter:false,
	//group:{'field':'group_name'},
	columns:[
		{header:'Url',dataIndex:'url',width:300,filter:{}},
		{header:'标题',dataIndex:'title',width:120,filter:{}},
		{header:'分类',dataIndex:'group_name',hidden:true},
		{header:'Params',dataIndex:'params',width:300,renderer:function(val){
			var arr = [];
			Ext.each(val,function(v,i) {
				var a = [];
				Ext.each(attr,function(el){
					if(v[el]) {
						a.push(v[el]);
					}
				});
				arr.push(v.name+(a.length?('('+a.join(',')+')'):''));
			});
			return arr.length?arr.join(' , '):'无';
		}},
		{header:'备注',dataIndex:'remark',width:250,filter:{}}
	]
});
var panel = new Ext.Panel({
	xtype:'panel',
	region:'east',
	split:true,
	collapseMode:'mini',
	collapsible:true,
	autoScroll:true,
	width:450
});
var viewport = new Ext.Viewport({
	layout:'border',
	items:[apiGrid,panel]
})

function refreshData(r) {
	r = r.json;
	var hasFile = false;
	var actionUrl = r.url;
	if(r.url.indexOf('http://') == -1) {
		actionUrl = list.siteUrl+r.url;
	}
	var html = '<style>.api-content{margin:10px 20px;}.api-content form{margin:15px 0;}\
		.api-content label{display:inline-block;width: 160px;}\
		.api-content p{padding: 0 0 10px 0;}.api-content button{margin-left: 200px;width: 75px;}\
		.api-content input{padding:5px;}.api-content pre {background-color: #eee;outline: 1px solid #ccc; padding: 5px;}\
		.api-content .string { color: green; }.api-content .number { color: darkorange; }\
		.api-content .boolean { color: blue; }.api-content .null { color: magenta; }\
		.api-content .key { color: red; }.api-content h1 {font-size: 18px;line-height: 26px;font-style: bold;}\
		.api-content em {color: red; } </style>\
		<div class="api-content"><h5 style="color:#999;">'+actionUrl+'</h5><h1>'+r.url+'</h1><form method="'+((r.config && r.config.method) ? r.config.method:'POST')+'" '
			+((r.config&&r.config.target) ? 'target="'+r.config.target+'"':'')+' action="'+actionUrl+'" id="frm_api"'
			+((r.config && r.config.target && r.config.target=="_blank") ? '' : 'onsubmit="return false;"')+'>';
	html += '<h2 style="color:#008800;padding:10px 0;">'+r.title+'</h2><p style="padding:0 0 10px;line-height:20px;">备注: '+(r.remark||'(无)')+'</p>';
	Ext.each(r.params,function(p,i){
		html += '<p><label>'+p.name+(p.label?('('+p.label+')'):'')+(p.optional?'':'<span style="color:red">*</span>')+':</label><input type="'+(p.type||'text')+'" name="'+p.name+'" value="'+(p.value||'')+'" size="20"></p>';
		if(p.type == 'file') {
			hasFile = true;
		}
	});
	html += '<button type="submit" >提交</button></form>';
	html += '<h3>返回数据</h3><pre id="result_content"></pre>';
	html += '</div>'
	panel.body.update(html);

	if(!(r.config && r.config.target && r.config.target=="_blank")) {
		Ext.get('frm_api').on('submit',function(e,t){
			Ext.Ajax.request({
			    form: 'frm_api',
			    isUpload: hasFile,
			    success:function(r) {
			    	r = eval('('+r.responseText+')');
			    	Ext.get('result_content').update(syntaxHighlight(r));
			    	if(r.addon.user) {
			    		user = r.addon.user;
				    	renderTitle(r.addon.user);
					}
				},
				failure:function(r) {
					if(r.code == 401) {
						alert('未登录');
						renderTitle('');
					}
				}
			});
		});
	}
}

apiGrid.on('rowclick',function(grid,rowIndex,e) {
	var r = grid.getStore().getAt(rowIndex);
	refreshData(r);
});

Ext.Ajax.defaultHeaders = {
    'app-version': '1.0',
    'Testpc':'1'
};

apiGrid.store.load();
apiGrid.store.on('load',function(s){
	var r = s.getAt(0);
	if(r) {
		refreshData(r);
		apiGrid.getSelectionModel().selectFirstRow();
	}
});

renderTitle(user);

function renderTitle(user) {
	if(user) {
		apiGrid.setTitle('用户:'+user.user_name);
	} else {
		apiGrid.setTitle('<span style="color:red;">未登录</span>');
	}
}


<?php include dirname(dirname(__FILE__)).'/admin/common/footer.php'; ?>